package com.jc.oms.controller.product;

import java.util.Date;
import java.util.HashMap;
import java.util.Map;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.alibaba.fastjson.JSONObject;
import com.jc.common.util.DateTimeUtil;
import com.jc.ds.model.SystemLogList;
import com.jc.framework.enums.Conclusion;
import com.jc.framework.enums.LogType;
import com.jc.oms.controller.BaseController;
import com.jc.service.jg.JgService;
import com.jc.service.model.jg.RequestBaseModel;
import com.jc.service.systemloglist.SystemLogListService;

@Controller()
@RequestMapping("product")
public class ProductController extends BaseController {
	
	
	@Autowired
	JgService jgService;
	
	@Autowired
	SystemLogListService systemLogListService;
	
	@SuppressWarnings("finally")
	@RequestMapping(value="/pull" , method = RequestMethod.POST , consumes = "application/json;charset=utf8")
	public @ResponseBody Map<String,Object> index(@RequestBody RequestBaseModel model){
		getLoger().error(DateTimeUtil.getDate(DateTimeUtil.LONG_TIME_FORMAT) + "-------->  接收一条产品信息 :  " +  JSONObject.toJSONString(model));
		SystemLogList loglist = new SystemLogList();
		Map<String,Object> _ret = new HashMap<String,Object>();
		try {
			jgService.receiveGoodsData(model);
			_ret.put("code", "10000");
			_ret.put("message", "成功");
			loglist.setConclusion(Conclusion.OK.getName());
		}catch (Exception e) {
			loglist.setConclusion(Conclusion.FAIL.getName());
			if (e != null && StringUtils.isNotBlank(e.getMessage())) {
				loglist.setExceptionMsg(
						e.getMessage().length() > 500 ? StringUtils.substring(e.getMessage(), 0, 500) : e.getMessage());
			}
			_ret.put("code", "9999");
			_ret.put("message", "推送失败");
			getLoger().error("接收吉贡产品,写入队列时发生异常, {},{}", e.getMessage(), e);
		}finally {
			loglist.setCreateTime(new Date());
			loglist.setLogType(LogType.RECEIVE_JG_PRODUCT.getName());
			loglist.setBusinessData(JSONObject.toJSONString(model));
			loglist.setLogTypeName(LogType.RECEIVE_JG_PRODUCT.getDesc());
			loglist.setRespText(JSONObject.toJSONString(_ret));
			systemLogListService.add(loglist, false);
			return _ret;
		}
	}
}